package lws.com.liwushuo.view.fragment.shouye;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import lws.com.liwushuo.R;

public class SearchMainActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener {


    private EditText searchEditText;
    private ImageView deleteAllText;
    private TextView cancleText;
    private ImageView backImg;
    private SearchFirstFragment firstFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouye_search_main_activity);
        //默认加载的是第一个页面
        firstFragment = new SearchFirstFragment();
        getSupportFragmentManager()
                .beginTransaction().replace(R.id.shouye_search_maincontainer, firstFragment)
                .commit();
        //初始化控件
        initView();
    }

    /**
     * 控件初始化
     */
    private void initView() {
        //搜索框
        searchEditText = (EditText) findViewById(R.id.shouye_search_main_searchEditText);
        searchEditText.addTextChangedListener(this);
        //搜索框输入字以后 出现的可以一次性清除字的按钮
        deleteAllText = (ImageView) findViewById(R.id.shouye_search_main_editText_deleteImg);
        deleteAllText.setOnClickListener(this);
        //取消按钮
        cancleText = (TextView) findViewById(R.id.shouye_search_first_topCancleText);
        cancleText.setOnClickListener(this);
        //返回 是在点击了搜索之后的 跳转替换第二个页面出现的
        backImg = (ImageView) findViewById(R.id.shouye_search_top_backImg);
        backImg.setOnClickListener(this);
    }


    /**
     * 搜索栏的监听
     *
     */

    /**
     * 文本输入之前
     *
     * @param s
     * @param start
     * @param count
     * @param after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if (s.length() == 0) {
            deleteAllText.setVisibility(View.GONE);
        } else {//只要有字体就得显示
            deleteAllText.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 文本发生改变
     *
     * @param s      输入框里面字符串
     * @param start
     * @param before
     * @param count  一次性输入的字数
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //只要字体改变就得请求网络自动匹配
        if (s.length() == 0) {
            deleteAllText.setVisibility(View.GONE);
        } else {//只要有字体就得显示
            deleteAllText.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void afterTextChanged(Editable s) {
        //文字改变后,获取文本框里面的关键字
        String keyWord = searchEditText.getText().toString();
        //替换页面
        //输入框输入内容时，自动匹配
        SearchWordCompletedFragment  completedFragment = new SearchWordCompletedFragment();
        Bundle bundle = new Bundle();
        bundle.putString("keyWord", keyWord);
        completedFragment.setArguments(bundle);
        //替换自动补全
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.shouye_search_maincontainer, completedFragment)
                .commit();
    }

    /**
     * 搜索栏的几个按钮监听
     * 左边返回，
     * 清除文字
     * 右边的取消
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shouye_search_first_topCancleText:
            case R.id.shouye_search_top_backImg:
                finish();
                break;
            case R.id.shouye_search_main_editText_deleteImg:
                searchEditText.setText("");//清空
                //替换到第一个界面
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.shouye_search_maincontainer, firstFragment)
                        .commit();
                break;
        }
    }

    public EditText getSearchEditText() {
        return searchEditText;
    }

    public void setSearchEditText(EditText searchEditText) {
        this.searchEditText = searchEditText;
    }

    public ImageView getDeleteAllText() {
        return deleteAllText;
    }

    public void setDeleteAllText(ImageView deleteAllText) {
        this.deleteAllText = deleteAllText;
    }

    public TextView getCancleText() {
        return cancleText;
    }

    public void setCancleText(TextView cancleText) {
        this.cancleText = cancleText;
    }

    public ImageView getBackImg() {
        return backImg;
    }

    public void setBackImg(ImageView backImg) {
        this.backImg = backImg;
    }

    public SearchFirstFragment getFirstFragment() {
        return firstFragment;
    }

    public void setFirstFragment(SearchFirstFragment firstFragment) {
        this.firstFragment = firstFragment;
    }
}
